package cn.edu.jxau.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.jxau.domain.reply;
import cn.edu.jxau.service.replyService;
import cn.itcast.commons.CommonUtils;

public class replyPostServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		replyService replyService=new replyService();
		
		String username=(String) request.getSession().getAttribute("username");
		//判断是否登录
		if(username==null){
			response.getWriter().write("<script type='text/javascript'>");
			response.getWriter().write("alert('你还没登录不能发帖')");
			response.getWriter().write("</script>");
			response.sendRedirect("login.jsp");
			return;
		}
		
		String reply_postid=request.getParameter("reply_postid");//得到帖子id
		String content = request.getParameter("editorValue");//得到回帖内容
		//得到回复谁的帖的用户名
		String[] str=content.split(":");
		String reply_user=str[0].replace("<p>@", "");
		
		reply reply=new reply();
		
		reply.setReply_content(content);
		reply.setReply_id(CommonUtils.uuid());
		reply.setReply_postid(reply_postid);
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		reply.setReply_time(df.format(new Date()));
		
		reply.setReply_user(username);
		
		replyService.addReply(reply);
		
		request.setAttribute("postId", reply_postid);
		request.getRequestDispatcher("findReplyServlet").forward(request, response);
		
	}
}
